package com.chy.blog_server.modular.web.controller;

import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.system.entity.SysUser;
import com.chy.blog_server.modular.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：控制台 controller
 * 作者：chenhuiyan
 * 时间：2020/01/18 00:51
 */
@Controller
@RequestMapping(value = "/web/webIndex")
public class webIndexController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/webIndex/";

    /**
     * 文章service
     */
    @Autowired
    private IBoArticleService boArticleService;

    /**
     * 相册service
     */
    @Autowired
    private IBoPhotoAlbumService photoAlbumService;

    /**
     * 照片/视频service
     */
    @Autowired
    private IBoPhotoService photoService;

    /**
     * 评论/留言service
     */
    @Autowired
    private IBoCommentService commentService;

    /**
     * 动态service
     */
    @Autowired
    private IBoDynamicService dynamicService;

    /**
     * 动态图片service
     */
    @Autowired
    private IBoDynamicImgService dynamicImgService;

    /**
     * 恋爱清单service
     */
    @Autowired
    private IBoLoveListService loveListService;

    /**
     * 约看书service
     */
    @Autowired
    private  IBoSeeBookService seeBookService;

    /**
     * 书名service
     */
    @Autowired
    private IBoBookService bookService;

    /**
     * 章节service
     */
    @Autowired
    private IBoBookListService bookListService;

    /**
     * 书目录service
     */
    @Autowired
    private IBoBookDirectoryService bookDirectoryService;

    /**
     *纪念日提醒service
     */
    @Autowired
    private IBoMarkDateService markDateService;

    /**
     * 点赞记录service
     */
    @Autowired
    private IBoPraiseHistoryService praiseHistoryService;

    /**
     * 主页面
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(){
        return "index";
    }

    /**
     * 控制台
     * @return
     */
    @RequestMapping(value = "/console")
    public String console(){
        return PREFIX + "console";
    }

    /**
     * 获取登录用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLoginUser")
    @ResponseBody
    public Map<String, Object> getLoginUser(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            SysUser sysUser = UserUtil.getUserFromSession(request); //获取登录的用户
            res.put("state", "200");
            res.put("message", "获取登录用户信息成功");
            res.put("loginUser", sysUser);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取登录用户信息失败");
        }
        return res;
    }

    /**
     * 首页基本数据统计模块
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDataStaticModel")
    public Map<String, Object> getDataStaticModel(){
        Map<String, Object> res = new HashMap<>();
        try{
            /**
             * map数据格式：
             * 标题：title
             * 数量：num
             * 备注：remark
             * 标记：done
             * 标记class：doneClass
             */
            List<Map<String, Object>> listMap = new ArrayList<>();

            Map<String, Object> params = new HashMap<>(); //参数

            //文章数量统计（通过并且没有被删除的文章）
            params.put("isPass", "1");
            params.put("delFlag", "0");
            Integer articleCount = boArticleService.getCount(params);
            Integer articleReadNumTotal = boArticleService.getReadNumTotal(); //文章阅读数量总数
            Integer articlePraiseNumTotal = boArticleService.getPraiseNumTotal(); //查询审核通过的文章赞数量总数
            Map<String, Object> articleModel = new HashMap<>();
            articleModel.put("title", "文章统计");
            articleModel.put("num", "<span style='color:#bf2f0e'>" + articleCount + "</span>");
            articleModel.put("remark", "阅读：<span style='color:#ed7901'>" + articleReadNumTotal + "</span>&nbsp;&nbsp;&nbsp;&nbsp;赞：<span style='color:#ed7901'>" + articlePraiseNumTotal + "</span>");
            articleModel.put("done", "文章");
            articleModel.put("doneClass", "label pull-right layui-bg-blue");
            listMap.add(articleModel);

            //相册数量统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer photoAlbumCount = photoAlbumService.getCount(params);
            Integer photoAlbumReadNumTotal = photoAlbumService.getReadNumTotal(); //查询阅读数量总数
            Integer photoAlbumPraiseNumTotal = photoAlbumService.getPraiseNumTotal(); //查询赞数量总数
            Map<String, Object> photoAlbumModel = new HashMap<>();
            photoAlbumModel.put("title", "相册统计");
            photoAlbumModel.put("num", "<span style='color:#bf2f0e'>" + photoAlbumCount + "</span>");
            photoAlbumModel.put("remark", "阅读：<span style='color:#ed7901'>" + photoAlbumReadNumTotal + "</span>&nbsp;&nbsp;&nbsp;&nbsp;赞：<span style='color:#ed7901'>" + photoAlbumPraiseNumTotal + "</span>");
            photoAlbumModel.put("done", "相册");
            photoAlbumModel.put("doneClass", "label pull-right layui-bg-cyan");
            listMap.add(photoAlbumModel);

            //照片视频统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer photoCount = photoService.getCount(params); //照片视频总数
            params.put("type", "0");
            Integer photoNumTotal = photoService.getCount(params); //查询照片总数
            params.put("type", "1");
            Integer photoToVideoNumTotal = photoService.getCount(params); //查询视频总数
            Map<String, Object> photoModel = new HashMap<>();
            photoModel.put("title", "照片视频统计");
            photoModel.put("num", "<span style='color:#bf2f0e'>" + photoCount + "</span>");
            photoModel.put("remark", "照片：<span style='color:#ed7901'>" + photoNumTotal + "</span>&nbsp;&nbsp;&nbsp;&nbsp;视频：<span style='color:#ed7901'>" + photoToVideoNumTotal + "</span>");
            photoModel.put("done", "照片视频");
            photoModel.put("doneClass", "label pull-right layui-bg-orange");
            listMap.add(photoModel);

            //评论/留言统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer commentCount = commentService.getCount(params); //评论/留言总数
            params.put("classify", "0");
            Integer articleCommentNum = commentService.getCount(params); //文章评论/留言总数
            params.put("classify", "1");
            Integer photoCommentNum = commentService.getCount(params); //相册评论/留言总数
            params.put("classify", "2");
            Integer qianTaiCommentNum = commentService.getCount(params); //前台评论/留言总数
            params.put("classify", "3");
            Integer diaryCommentNum = commentService.getCount(params); //日记评论/留言总数
            params.put("classify", "4");
            Integer spaceCommentNum = commentService.getCount(params); //个人空间评论/留言总数
            Map<String, Object> commentModel = new HashMap<>();
            commentModel.put("title", "评论/留言统计");
            commentModel.put("num", "<span style='color:#bf2f0e'>" + commentCount + "</span>");
            commentModel.put("remark", "文章:<span style='color:#ed7901'>" + articleCommentNum +
                    "</span> 相册:<span style='color:#ed7901'>" + photoCommentNum +
                    "</span> 前台:<span style='color:#ed7901'>" + qianTaiCommentNum +
                    "</span> 日记:<span style='color:#ed7901'>" + diaryCommentNum +
                    "</span> 空间:<span style='color:#ed7901'>" + spaceCommentNum + "</span>");
            commentModel.put("done", "评论/留言");
            commentModel.put("doneClass", "label pull-right layui-bg-green");
            listMap.add(commentModel);

            res.put("modelList", listMap);
            res.put("message", "基本数据统计模块获取成功");
            res.put("state", "200");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "基本数据统计模块获取失败");
        }
        return res;
    }

    /**
     * 首页空间数据统计模块
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSpaceDataStaticModel")
    public Map<String, Object> getSpaceDataStaticModel(){
        Map<String, Object> res = new HashMap<>();
        try{
            /**
             * map数据格式：
             * 标题：title
             * 数量：num
             * 备注：remark
             * 标记：done
             * 标记class：doneClass
             */
            List<Map<String, Object>> listMap = new ArrayList<>();

            Map<String, Object> params = new HashMap<>(); //参数

            //动态数量统计
            params.put("delFlag", "0");
            Integer dynamicCount = dynamicService.getCount(params);

            params.put("type", "0");
            Integer dynamicLoveTotal_1 = dynamicService.getCount(params); //恋爱清单
            params.put("type", "2");
            Integer dynamicLoveTotal_2 = dynamicService.getCount(params); //发起恋爱清单
            Integer dynamicLoveTotal = dynamicLoveTotal_1 + dynamicLoveTotal_2;

            params.put("type", "1");
            Integer dynamicSeeBookTotal_1 = dynamicService.getCount(params); //看书清单
            params.put("type", "3");
            Integer dynamicSeeBookTotal_2 = dynamicService.getCount(params); //发起看书清单
            Integer dynamicSeeBookTotal = dynamicSeeBookTotal_1 + dynamicSeeBookTotal_2;

            params.put("type", "4");
            Integer dynamicOtherTotal = dynamicService.getCount(params); //其他

            params = new HashMap<>();
            params.put("delFlag", "0");
            params.put("fileType", "0");
            Integer dynamicImgTotal = dynamicImgService.getCount(params); //图片
            params.put("fileType", "1");
            Integer dynamicVideoTotal = dynamicImgService.getCount(params); //视频

            Map<String, Object> articleModel = new HashMap<>();
            articleModel.put("title", "动态统计");
            articleModel.put("num", "<span style='color:#bf2f0e'>" + dynamicCount + "</span>");
            articleModel.put("remark", "恋爱:<span style='color:#ed7901'>" + dynamicLoveTotal +
                    "</span> 看书:<span style='color:#ed7901'>" + dynamicSeeBookTotal +
                    "</span> 其他:<span style='color:#ed7901'>" + dynamicOtherTotal +
                    "</span> 图片/视频:<span style='color:#ed7901'>" + dynamicImgTotal + "/" + dynamicVideoTotal + "</span>");
            articleModel.put("done", "动态");
            articleModel.put("doneClass", "label pull-right layui-bg-red");
            listMap.add(articleModel);

            //恋爱清单数量统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer loveListCount = loveListService.getCount(params); //恋爱清单
            Integer seeBookCount = seeBookService.getCount(params); //约看书
            Integer loveCount = loveListCount + seeBookCount;
            Integer sweetTotal = loveListService.getSweet() + seeBookService.getSweet(); //亲密度
            Map<String, Object> photoAlbumModel = new HashMap<>();
            photoAlbumModel.put("title", "恋爱清单统计");
            photoAlbumModel.put("num", "<span style='color:#bf2f0e'>" + loveCount + "</span>");
            photoAlbumModel.put("remark", "恋爱：<span style='color:#ed7901'>" + loveListCount +
                    "</span>&nbsp;&nbsp;&nbsp;&nbsp;看书：<span style='color:#ed7901'>" + seeBookCount +
                    "</span>&nbsp;&nbsp;&nbsp;&nbsp;亲密度：<span style='color:#ed7901'>" + sweetTotal + "</span>");
            photoAlbumModel.put("done", "恋爱清单");
            photoAlbumModel.put("doneClass", "label pull-right layui-bg-orange");
            listMap.add(photoAlbumModel);

            //书籍统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer bookCount = bookService.getCount(params); //书名
            Integer bookListCount = bookListService.getCount(params); //章节
            Integer bookDirectoryCount = bookDirectoryService.getCount(params); //书目录
            Map<String, Object> photoModel = new HashMap<>();
            photoModel.put("title", "书籍统计");
            photoModel.put("num", "<span style='color:#bf2f0e'>" + bookCount + "</span>");
            photoModel.put("remark", "书目录：<span style='color:#ed7901'>" + bookDirectoryCount +
                    "</span>&nbsp;&nbsp;&nbsp;&nbsp;章节：<span style='color:#ed7901'>" + bookListCount + "</span>");
            photoModel.put("done", "书籍");
            photoModel.put("doneClass", "label pull-right layui-bg-green");
            listMap.add(photoModel);

            //纪念日统计
            params = new HashMap<>();
            params.put("delFlag", "0");
            Integer markDateCount = markDateService.getCount(params); //纪念日总数
            Map<String, Object> commentModel = new HashMap<>();
            commentModel.put("title", "纪念日统计");
            commentModel.put("num", "<span style='color:#bf2f0e'>" + markDateCount + "</span>");
            commentModel.put("remark", "公历、农历");
            commentModel.put("done", "纪念日");
            commentModel.put("doneClass", "label pull-right layui-bg-black");
            listMap.add(commentModel);

            res.put("spaceModelList", listMap);
            res.put("message", "空间数据统计模块获取成功");
            res.put("state", "200");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "空间数据统计模块获取失败");
        }
        return res;
    }

    /**
     * 首页点赞记录模块
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPraiseStaticModel")
    public Map<String, Object> getPraiseStaticModel(){
        Map<String, Object> res = new HashMap<>();
        try{
            /**
             * map数据格式：
             * 标题：title
             * 时间：time
             */
            List<Map<String, Object>> listMap = new ArrayList<>();

            Map<String, Object> params = new HashMap<>(); //参数

            //动态数量统计
            params.put("size", 0);
            params.put("limit", 6);
            List<Map<String, Object>> praiseList = praiseHistoryService.list(params);

            for(Map map: praiseList){
                //文章
                if(Integer.parseInt(map.get("type") + "") == 0){
                    String title = map.get("ipAddress") + "点赞了文章" + "<span style='color:#f7b304'>《" + map.get("articleTitle") + "》</span>";
                    Map<String ,Object> modelMap = new HashMap<>();
                    modelMap.put("title", title);
                    modelMap.put("time", map.get("time"));
                    listMap.add(modelMap);

                    //评论
                }else if(Integer.parseInt(map.get("type") + "") == 1){
                    String title = map.get("ipAddress") + "点赞了" + "<span style='color:#f7b304'>" + map.get("commentUserName") + "</span>的评论";
                    Map<String ,Object> modelMap = new HashMap<>();
                    modelMap.put("title", title);
                    modelMap.put("time", map.get("time"));
                    listMap.add(modelMap);

                    //2相册
                }else if(Integer.parseInt(map.get("type") + "") == 2){
                    String title = map.get("ipAddress") + "点赞了相册" + "<span style='color:#f7b304'>《" + map.get("photoAlbumName") + "》</span>";
                    Map<String ,Object> modelMap = new HashMap<>();
                    modelMap.put("title", title);
                    modelMap.put("time", map.get("time"));
                    listMap.add(modelMap);
                }
            }

            res.put("praiseModelList", listMap);
            res.put("message", "点赞记录模块获取成功");
            res.put("state", "200");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "点赞记录模块获取失败");
        }
        return res;
    }

}
